﻿@model Zatify.WebUI.Models.PromotionCategoriesViewModel
@{
    ViewBag.Title = "Promotion";
    ViewBag.ContentTitle = "Promotion";
    Layout = "~/Views/Shared/_ContentLayout.cshtml";
}
@section Styles
{
    <style type="text/css">
        .zatify_panel_btn
        {
            width: 100%;
        }
        .inner_container
        {
            float: none;
        }
        .categories-row
        {
            margin-top: 10px;
        }
        .categories_inline
        {
            display: inline;
            margin-right: 10px;
            padding: 8px 10px;
            width: 818px;
        }
        .categories_line a
        {
            text-decoration: underline;
        }
        .validation-summary-errors
        {
            margin-top: 0px;
            margin-bottom: 10px;
        }
    </style>
}
@section Scripts
{
    <script type="text/javascript">

        var hiddenCategoriesIds;
        var catsIds = [];

        $(document).ready(function () {

            hiddenCategoriesIds = $("#SelectedCategoriesIds");

            if (hiddenCategoriesIds != "" && hiddenCategoriesIds.val().length > 0)
                catsIds = hiddenCategoriesIds.val().split(",");
        })

        function addCategory(e, id) {

            if ($(e).attr("checked")) {

                if ($.inArray(id, catsIds) == -1)
                    catsIds.push(id);

                if (catsIds.length == 1)
                    hiddenCategoriesIds.val(id);
                else
                    hiddenCategoriesIds.val(catsIds.join(","));

                $(".chkCategory_" + id).attr("checked", "checked");

            }
            else {

                catsIds = jQuery.grep(catsIds, function (value) {
                    return value != id;
                });

                $(".chkCategory_" + id).removeAttr("checked");

                hiddenCategoriesIds.val(catsIds.join(","));
            }
        }

        function addSingleCategory() {

            $.each($("input[type=radio]"), function (index, item) {

                addCategory(item, $(item).attr("id"));
            });
        }
    </script>
}
<div class="business_interface_area">
    <div class="business_profile_categories">
        @if (@TempData["Error"] != null)
        {
            <div class="validation-summary-errors">
                <ul>
                    <li>@TempData["Error"]</li></ul>
            </div>
        }
        <div class="campaign_category_selection">
            Your Campaign Category Selection</div>
        @if (Model.CurrentCategory != null)
        {
            <div style="margin-bottom: 15px; margin-top: 5px;" class="zatify_panel_btn">
                <div class="categories_line categories_inline category_navbar">
                    <a href="@Url.Action("PromotionCategories", "Promotions", new { promotionId = Model.PromotionId })">
                        All Categories</a>
                </div>
                @Html.CategoryBreadCrumbsAsButtons(Model.CurrentCategory, Model.SelectedCategories,
                    x => Url.Action("PromotionCategories", "Promotions", new { promotionId = Model.PromotionId, id = x }))
            </div>
        }
        @if (Model.CurrentCategory == null)
        {
            <div class="zatify_panel_btn">
                @foreach (var cat in Model.Categories)
                {
                    <div class="categories-row">
                        @Html.CategoryBreadCrumbsAsButtons(cat, Model.SelectedCategories, x => Url.Action("PromotionCategories", "Promotions", new { promotionId = Model.PromotionId, id = x }))
                    </div>
                    <br />
                }
                @foreach (var cat in Model.SelectedCategories)
                {
                    <div class="categories-row">
                        @Html.CategoryBreadCrumbsAsButtons(cat, Model.SelectedCategories, x => Url.Action("PromotionCategories", "Promotions", new { promotionId = Model.PromotionId, id = x }))
                        <a class="minus_btn" href="@Url.Action("PromotionCategoriesRemove", new { id = cat.CategoryId })" onclick="return confirm('You want to remove this category?');" style="display: inline;">
                            <img class="category_control" alt="" src="@Url.Content("~/Content/zatify/images/minus_btn.png")" />
                        </a>
                    </div>
                    <br />
                }
            </div>
        }
        else
        {
            for (int i = 0; i < Model.Categories.Count; i++)
            {
            <div class="categories_line business_reg_step3_line">
                <label>
                    @if (Model.Categories[i].ChildCategories.Count() > 0)
                    {
                        <a href="@Url.Action("PromotionCategories", "Promotions", new { promotionId = Model.PromotionId, id = Model.Categories[i].CategoryId })">@Model.Categories[i].CategoryName</a>
                    }
                    else
                    {
                        @Model.Categories[i].CategoryName
                    }
                    @if (!Model.Categories[i].IsSingleSelection)
                    {
                        if (Model.Categories[i].IsSelectable())
                        {
                        <input class="categories_checkbox" onchange="addCategory(this, @Model.Categories[i].CategoryId)" type="checkbox" 
                        @(Model.SelectedCategories.Any(c => c.CategoryId == Model.Categories[i].CategoryId) ? "checked='checked' disabled='disabled'" : "")/>
                        }
                    }
                    else
                    {
                        <input id="@(Model.Categories[i].CategoryId)" class="categories_checkbox" type="radio" name="singleCategory" onclick="addSingleCategory()" 
                            @(Model.SelectedCategories.Any(c => c.CategoryId == Model.Categories[i].CategoryId) ? "checked='checked'" : "") />
                    }
                </label>
            </div>
            }
        }
        <div class="clearfix">
        </div>
        <p class="campaign_cost_rate">
            Campaign Cost: $@Model.FlatFee.ToString("0.00")/flat and $@Model.BaseCost.ToString("0.00")/push</p>
        @if (Model.CurrentCategory != null)
        {
            using (Html.BeginForm("PromotionCategoriesAdd", "Promotions"))
            {
            @Html.HiddenFor(m => m.PromotionId)
            @Html.Hidden("SelectedCategoriesIds", Model.SelectedCategoriesIds)
            <input type="submit" value="Add" class="blue_btn" style="margin-left: 5px;" />
            }
        }
        else
        {
            using (Html.BeginForm())
            {
            @Html.HiddenFor(m => m.PromotionId)
            @Html.Hidden("SelectedCategoriesIds")
            <input type="submit" value="Next" class="blue_btn" style="margin-left: 5px; margin-bottom: 10px;" />
            <br />
            @Html.ValidationSummary()
            }
        }
    </div>
    <div class="clearfix">
    </div>
</div>
